package org.eclipse.acceleo.module.sample.service;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Stereotype;

public class StereotypeService {
	public static Stereotype getStereotype(Element element,
			String stereotypeName) {
		EList<Stereotype> stereotypes = element.getAppliedStereotypes();
		for (Iterator<Stereotype> i = stereotypes.iterator(); i.hasNext();) {
			Stereotype stereotype = i.next();
			System.out.println("stereotype.getName() = '" + stereotype.getName() + "'");
			if (stereotype.getName().equals(stereotypeName)) {
				return stereotype;
			}
		}
		return null;
	}

	public static boolean hasStereotype(Element element, String stereotypeName) {
		Stereotype stereotype = getStereotype(element, stereotypeName);
		return (stereotype != null);
	}

	public static boolean hasContainedValue(Element element, String stereotypeName, String stereotypProp, Object value) {
		Stereotype stereotype = getStereotype(element, stereotypeName);
		Object containedValue = element.getValue(stereotype, stereotypProp);
		boolean result = false;
		if(containedValue instanceof List) {
			for(Object eleValue : ((List<?>)containedValue)) {
				if(eleValue == value) {
					result = true;
				}
			}
		} else {
			result = containedValue == value;
		}
		return result;
	}
}
